package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IClientCrmProjetDetailService;
import com.mdd.admin.service.ICustomerProjectService;
import com.mdd.admin.validate.CustomerProjectCreateValidate;
import com.mdd.admin.validate.CustomerProjectSearchValidate;
import com.mdd.admin.validate.CustomerProjectUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.CustomerProjectDetailVo;
import com.mdd.admin.vo.CustomerProjectListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.CustomerProject;
import com.mdd.common.mapper.CustomerProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户与项目匹配实现类
 *
 * @author LikeAdmin
 */
@Service
public class CustomerProjectServiceImpl implements ICustomerProjectService {

    @Resource
    CustomerProjectMapper customerProjectMapper;



    @Resource
    IClientCrmProjetDetailService projectService;



    /**
     * 客户与项目匹配列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CustomerProjectListedVo>
     * @author LikeAdmin
     */
    @Override
    public PageResult<CustomerProjectListedVo> list(PageValidate pageValidate, CustomerProjectSearchValidate searchValidate) {
        Integer page = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CustomerProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        customerProjectMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "=:clientId@client_id:int",
                "=:projectId@project_id:int",
                "=:created:str",
        });

        IPage<CustomerProject> iPage = customerProjectMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CustomerProjectListedVo> list = new LinkedList<>();
        for (CustomerProject item : iPage.getRecords()) {
            CustomerProjectListedVo vo = new CustomerProjectListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setProject(projectService.detail(item.getProjectId()));

            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户与项目匹配详情
     *
     * @param id 主键参数
     * @return CustomerProject
     * @author LikeAdmin
     */
    @Override
    public CustomerProjectDetailVo detail(Integer id) {
        CustomerProject model = customerProjectMapper.selectOne(
                new QueryWrapper<CustomerProject>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CustomerProjectDetailVo vo = new CustomerProjectDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户与项目匹配新增
     *
     * @param createValidate 参数
     * @author LikeAdmin
     */
    @Override
    public void add(CustomerProjectCreateValidate createValidate) {
        CustomerProject model = new CustomerProject();
        model.setClientId(createValidate.getClientId());
        model.setProjectId(createValidate.getProjectId());
        model.setCreated(createValidate.getCreated());
        customerProjectMapper.insert(model);
    }

    @Override
    public void add(Integer customerId, List<Integer> projectIds) {
        CustomerProjectSearchValidate searchValidate = new CustomerProjectSearchValidate();
        searchValidate.setClientId(customerId);
        QueryWrapper<CustomerProject> queryWrapper = new QueryWrapper<>();
        customerProjectMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "=:clientId@client_id:int",
        });

        List<CustomerProject> customerProjectList = customerProjectMapper.selectList(queryWrapper);
        List<Integer> addProjectIds=customerProjectList.stream().map(x->x.getProjectId()).collect(Collectors.toList());

        projectIds.stream().filter(id-> !addProjectIds.contains(id)).collect(Collectors.toList()).forEach(id->{
            CustomerProjectCreateValidate createValidate=new CustomerProjectCreateValidate();
            createValidate.setCreated(new Date());
            createValidate.setClientId(customerId);
            createValidate.setProjectId(id);
            this.add(createValidate);
        });



    }

    /**
     * 客户与项目匹配编辑
     *
     * @param updateValidate 参数
     * @author LikeAdmin
     */
    @Override
    public void edit(CustomerProjectUpdateValidate updateValidate) {
        CustomerProject model = customerProjectMapper.selectOne(
                new QueryWrapper<CustomerProject>()
                        .eq("id", updateValidate.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setClientId(updateValidate.getClientId());
        model.setProjectId(updateValidate.getProjectId());
        model.setCreated(updateValidate.getCreated());
        customerProjectMapper.updateById(model);
    }

    /**
     * 客户与项目匹配删除
     *
     * @param id 主键ID
     * @author LikeAdmin
     */
    @Override
    public void del(Integer id) {
        CustomerProject model = customerProjectMapper.selectOne(
                new QueryWrapper<CustomerProject>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        customerProjectMapper.delete(new QueryWrapper<CustomerProject>().eq("id", id));
    }

}
